<template>
  <div>
    <!-- 头部面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 添加按钮 -->
      <el-button type="primary" @click="addcate">添加分类</el-button>
      <!-- 表格区域 -->
      <tree-table
        :data="catelist"
        :columns="columns"
        :selectionType="false"
        :expandType="false"
        showIndex
        indexText="#"
        border
      >
        <!-- 是否有效模板 -->
        <template v-slot:isok="scope">
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted === false"
            style="color: lightgreen"
          ></i>
          <i class="el-icon-error" v-else></i>
        </template>
        <!-- 排序模板 -->
        <template v-slot:order="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="success" size="mini" v-if="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag type="warning" size="mini" v-if="scope.row.cat_level === 2"
            >三级</el-tag
          >
        </template>
        <!-- 操作模板 -->
        <template v-slot:opt="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="updatacate(scope.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="delcatebyid(scope.row)"
            >删除</el-button
          >
        </template>
      </tree-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="querinfo.pagenum"
        :page-sizes="[2, 4, 5, 6]"
        :page-size="querinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totle"
      >
      </el-pagination>
      <!-- 添加分类的对话框 -->
      <el-dialog
        title="添加分类"
        :visible.sync="addcatedialog"
        width="50%"
        @close="closecateadd"
      >
        <el-form
          :model="addcateinfo"
          :rules="addcaterule"
          ref="addcate"
          label-width="100px"
        >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="addcateinfo.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类">
            <!-- options指定数据源 -->
            <!-- props指定配置对象 -->
            <el-cascader
              v-model="seletedkeys"
              :options="catalllist"
              :props="cascaderpors"
              @change="catevaluechange"
              :clearable="true"
            ></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addcatedialog = false">取 消</el-button>
          <el-button type="primary" @click="yesaddcate">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 编辑按钮对话框 -->
      <el-dialog title="编辑分类名称" :visible.sync="isupdatanone" width="50%">
        <el-form
          :model="updatacateinfo"
          :rules="updatacaterules"
          ref="updatacete"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="updatacateinfo.cat_name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="isupdatanone = false">取 消</el-button>
          <el-button type="primary" @click="querupdatacate">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog
        title="删除分类"
        :visible.sync="delcate"
        width="50%"
      >
        <h4>是否删除该分类名称</h4>
        <span slot="footer" class="dialog-footer">
          <el-button @click="delcate = false">取 消</el-button>
          <el-button type="primary" @click="querdelcate"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //商品分类的列表
      catelist: [],
      //总数
      totle: 0,
      //查询参数
      querinfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      //table指定列的定义
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
          width: "200px",
        },
        {
          label: "是否有效",
          //将当前列定义为模板列
          type: "template",
          //当前列使用的模板名称
          template: "isok",
          width: "200px",
        },
        {
          label: "排序",
          //将当前列定义为模板列
          type: "template",
          //当前列使用的模板名称
          template: "order",
          width: "150px",
        },
        {
          label: "操作",
          //将当前列定义为模板列
          type: "template",
          //当前列使用的模板名称
          template: "opt",
          width: "200px",
        },
      ],
      //添加分类对话框的显示与隐藏
      addcatedialog: false,
      //添加分类的数据
      addcateinfo: {
        //分类名称
        cat_name: "",
        //分类的父级id
        cat_pid: 0,
        //分类的层级
        //默认的等级是一级分类
        cat_level: 0,
      },
      //添加分类的数据校验规则
      addcaterule: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
      //父级分类的列表数据
      catalllist: [],
      //级联选择器  cascader 的配置对象
      cascaderpors: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
        expandTrigger: "hover",
        checkStrictly: true,
      },
      //选中的父级分类的id
      seletedkeys: [],
      isupdatanone: false,
      //更新的表单数据
      updatacateinfo: {
        cat_name: "",
        cat_id: 0,
      },
      //更新的表单数据的验证规则
      updatacaterules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
      //删除分类按钮弹窗
      delcate:false,
      byid:0
    };
  },
  created() {
    this.getcatelist();
  },
  methods: {
    //获取数据
    async getcatelist() {
      const { data: res } = await this.$http.get("categories", {
        params: this.querinfo,
      });
      if (res.meta.status != 200) return this.$message.error("获取商品失败");
      this.catelist = res.data.result;
      this.totle = res.data.total;
      this.$message.success("获取成功");
    },
    //监听pagesize改变的数据
    handleSizeChange(newsize) {
      this.querinfo.pagesize = newsize;
      this.getcatelist();
    },
    //监听pagenum改变
    handleCurrentChange(newsum) {
      this.querinfo.pagenum = newsum;
      this.getcatelist();
    },
    //添加分类按钮
    addcate() {
      this.addcatedialog = true;
      //获取父级分类列表
      this.getfristlist();
    },
    //获取父级分类的数据列表
    async getfristlist() {
      const { data: res } = await this.$http.get("categories", {
        params: { type: 2 },
      });
      if (res.meta.status !== 200)
        return this.$message.error("获取分类列表失败");
      this.catalllist = res.data;
      console.log(this.catalllist);

      this.$message.success("获取分类列表成功");
    },
    //级联选择器选择项发生改变
    catevaluechange() {
      if (this.seletedkeys.length > 0) {
        this.addcateinfo.cat_pid =
          this.seletedkeys[this.seletedkeys.length - 1];
        this.addcateinfo.cat_level = this.seletedkeys.length;
        return;
      }
      this.addcateinfo.cat_pid = 0;
      this.addcateinfo.cat_level = 0;
    },
    //确定按钮事件
    yesaddcate() {
      this.$refs.addcate.validate(async (valid) => {
        if (!valid) return this.$message.error("请确定数据的合法性");
        const { data: res } = await this.$http.post(
          "categories",
          this.addcateinfo
        );
        if (res.meta.status !== 201) return this.$message.error("添加分类失败");
        this.getfristlist();
        this.getcatelist();
        this.$message.success("添加成功");
      });
      this.addcatedialog = false;
      console.log(this.addcateinfo);
    },
    //关闭添加分类对话框清空表单里面的数据
    closecateadd() {
      this.$refs.addcate.resetFields();
      this.seletedkeys = [];
      this.addcateinfo.cat_pid = 0;
      this.addcateinfo.cat_level = 0;
    },
    //编辑按钮
    updatacate(row) {
      this.updatacateinfo.cat_name = row.cat_name;
      this.updatacateinfo.cat_id = row.cat_id;
      console.log(row);
      console.log(this.updatacateinfo);
      this.isupdatanone = true;
    },
    //确认修改分类名称按钮
    async querupdatacate() {
      console.log(this.updatacateinfo);
      const { data: res } = await this.$http.put(
        `categories/${this.updatacateinfo.cat_id}`,
        { cat_name: this.updatacateinfo.cat_name }
      );
      if (res.meta.status != 200) return this.$message.error("修改失败");
      this.$message.success("修改成功");
      this.getcatelist();
      this.isupdatanone = false;
    },
    //删除按钮
    delcatebyid(row) {
      this.byid = row.cat_id
      console.log(this.byid);
      this.delcate = true
    },
   async querdelcate(){
      const {data:res} = await this.$http.delete(`categories/${this.byid}`)
     if(res.meta.status!==200) return this.$message.error('删除失败')
     this.$message.success('删除成功')
    this.getcatelist()
      this.delcate = false
    }
  },
};
</script>

<style  scoped>
.el-card {
  margin-top: 10px;
}
.el-button {
  margin-bottom: 10px;
}
.el-cascader {
  width: 100%;
}
</style>